package com.wht.sip.core.listener;

import com.wht.sip.core.context.SipProcessorContext;
import com.wht.sip.core.transmit.message.SipMessageProcessor;
import gov.nist.javax.sip.message.SIPResponse;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Managed;
import org.noear.solon.scheduling.annotation.Async;

import javax.sip.*;

/**
 * @author by HaiTao.Wang on 2025/9/15.
 */
@Slf4j
@Managed
public class SipListenerImpl implements SipListener {

    /**
     * 处理RequestEvent事件
     *
     * @param requestEvent 请求事件
     */
    @Async
    @Override
    public void processRequest(RequestEvent requestEvent) {

        String method = requestEvent.getRequest().getMethod();
        System.err.println("processRequest:" + method);

        SipMessageProcessor messageProcessor = SipProcessorContext.requestProcessor.get(method);
        if (messageProcessor != null) {
            messageProcessor.processRequest(requestEvent);
        }

    }

    /**
     * 处理ResponseEvent事件
     *
     * @param responseEvent 响应事件
     */
    @Async
    @Override
    public void processResponse(ResponseEvent responseEvent) {

        //获取method
        SIPResponse response = (SIPResponse)responseEvent.getResponse();
        String method = response.getCSeqHeader().getMethod();
        SipProcessorContext.responseProcessor.get(method).processResponse(responseEvent);

    }

    /**
     * 处理TimeoutEvent事件
     *
     * @param timeoutEvent 超时事件
     */
    @Override
    public void processTimeout(TimeoutEvent timeoutEvent) {

    }

    /**
     * 处理IOExceptionEvent事件
     *
     * @param exceptionEvent IO异常事件
     */
    @Override
    public void processIOException(IOExceptionEvent exceptionEvent) {

    }

    /**
     * 处理TransactionTerminatedEvent事件
     *
     * @param transactionTerminatedEvent 事务终止事件
     */
    @Override
    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {

    }

    /**
     * 处理DialogTerminatedEvent事件
     *
     * @param dialogTerminatedEvent 对话终止事件
     */
    @Override
    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {

    }
}
